﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;
using System.Globalization;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;


namespace KegManager
{
    //[ValueConversion(typeof(System.Data.Linq.Binary), typeof(ImageSource))]
    //public sealed class BinaryLabelConverter : IValueConverter
    //{
    //    static BitmapImage NoLabelImage = null;

    //    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    //    {
    //        if (value != null)
    //        {
    //            System.Data.Linq.Binary BinaryData = value as System.Data.Linq.Binary;
    //            Byte[] ByteArray = BinaryData.ToArray();
    //            BitmapImage bmp = new BitmapImage();
    //            bmp.BeginInit();
    //            bmp.StreamSource = new MemoryStream(ByteArray);
    //            bmp.EndInit();

    //            return bmp;
    //        }

    //        if (NoLabelImage == null)
    //        {
    //            // Load the unknown label image into memory
    //            NoLabelImage = new BitmapImage(new Uri("pack://application:,,,/KegManager;component/resources/unknownlabel.png"));
    //        }

    //        return NoLabelImage;
    //    }

    //    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    //    {
    //        throw new NotImplementedException();
    //    }
    //}
    [ValueConversion(typeof(String), typeof(ImageSource))]
    public sealed class UserImageConverter : IValueConverter
    {
        static BitmapImage NoAvatarImage = null;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                System.Data.Linq.Binary BinaryData = value as System.Data.Linq.Binary;
                Byte[] ByteArray = BinaryData.ToArray();
                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.StreamSource = new MemoryStream(ByteArray);
                bmp.EndInit();

                return bmp;
            }

            if (NoAvatarImage == null)
            {
                // Load the unknown label image into memory
                NoAvatarImage = new BitmapImage(new Uri("pack://application:,,,/KegManager;component/Images/UnknownAvatar.png"));
            }

            return NoAvatarImage;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(String), typeof(ImageSource))]
    public sealed class PathLabelConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(ImageSource))
                throw new InvalidOperationException("Target type must be System.Windows.Media.ImageSource.");

            BitmapImage bmp;

            // Default to an unknown label
            String path = "pack://application:,,,/KegManager;component/Images/GenericLabel.jpg";

            // If the path is valid, use that.
            if (value != null)
            {
                String path2 = System.IO.Path.Combine(Properties.Settings.Default.LabelDirectory, (String)value);
                if (System.IO.File.Exists(path2))
                {
                    path = path2;
                }
            }

            try
            {
                bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.UriSource = new Uri(path);
                bmp.EndInit();
                return bmp;
            }
            catch (Exception ex)
            {
                return DependencyProperty.UnsetValue;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
